package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.entry.po.Department;
import com.example.human_resource_management.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    // 查询所有部门
    @GetMapping("/all")
    public ResponseData<List<Department>> getAllDepartments() {
        List<Department> departments = departmentService.getAllDepartments();
        return ResponseData.ok(departments);
    }

    // 按ID查询部门
    @GetMapping("/{deptId}")
    public ResponseData<Department> getDepartmentById(@PathVariable Long deptId) {
        Department department = departmentService.getDepartmentById(deptId);
        return ResponseData.ok(department);
    }

    // 添加部门
    @PostMapping("/add")
    public ResponseData<String> addDepartment(@RequestBody Department department) {
        boolean success = departmentService.addDepartment(department);
        return success ? ResponseData.ok("添加部门成功") : ResponseData.warn("添加部门失败");
    }

    // 更新部门
    @PutMapping("/update")
    public ResponseData<String> updateDepartment(@RequestBody Department department) {
        boolean success = departmentService.updateDepartment(department);
        return success ? ResponseData.ok("更新部门成功") : ResponseData.warn("更新部门失败");
    }

    // 删除部门
    @DeleteMapping("/delete/{deptId}")
    public ResponseData<String> deleteDepartment(@PathVariable Long deptId) {
        boolean success = departmentService.deleteDepartment(deptId);
        return success ? ResponseData.ok("删除部门成功") : ResponseData.warn("删除部门失败");
    }
}